<template>
	<el-dialog v-model="visible" :title="dataForm.id ? '编辑算法' : '新增算法'" :close-on-click-modal="false">
		<el-form label-width="130" :model="dataForm" :rules="dataRules" ref="dataFormRef">
			<el-form-item label="选择算法" prop="algoCode">
				<el-select v-model="dataForm.algoCode" :disabled="dataForm.id ? true : false" placeholder="请选择算法" style="width: 100%">
					<el-option label="余弦相似度" :value="'1'"></el-option>
					<el-option label="精准匹配" :value="'2'"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="算法说明" prop="algoDesc">
				<el-input v-model="dataForm.algoDesc" type="textarea" :maxlength="200" autosize show-word-limit placeholder="请输入算法说明"></el-input>
			</el-form-item>

			<el-form-item label="适用说明" prop="instructions">
				<el-input v-model="dataForm.instructions" :maxlength="200" autosize show-word-limit type="textarea" placeholder="请输入适用说明"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="btnLoading" @click="saveFormBtn">确定</el-button>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { saveAlgorithm } from '@/api/patient-primary/algorithm-mana'
import { ElMessage } from 'element-plus'
const visible = ref(false)
const btnLoading = ref(false)
const emit = defineEmits(['close'])

const dataFormRef = ref()

const dataForm = reactive({
	algoCode: '',
	algoDesc: '',
	instructions: ''
})
const dataRules = ref({
	algoCode: [
		{
			required: true,
			message: '请选择算法',
			trigger: 'change'
		}
	],
	// algoDesc: [
	// 	{
	// 		validator: (rule, value, callback) => {
	// 			if (value && value.length) {
	// 				if (value.length > 200) {
	// 					callback(new Error('算法说明不能超过200个字符'))
	// 					dataForm.algoDesc = value.slice(0, 200)
	// 				} else {
	// 					callback()
	// 				}
	// 			} else {
	// 				callback()
	// 			}
	// 		}
	// 	}
	// ],
	// instructions: [
	// 	{
	// 		validator: (rule, value, callback) => {
	// 			if (value && value.length) {
	// 				if (value.length > 200) {
	// 					callback(new Error('适用说明不能超过200个字符'))
	// 					dataForm.instructions = value.slice(0, 200)
	// 				} else {
	// 					callback()
	// 				}
	// 			} else {
	// 				callback()
	// 			}
	// 		}
	// 	}
	// ]
})

const init = data => {
	btnLoading.value = false
	visible.value = true

	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (data) {
		Object.assign(dataForm, data)
	}
}

const saveFormBtn = () => {
	dataFormRef.value.validate(valid => {
		if (!valid) return
		btnLoading.value = true
		// 保存逻辑
		saveAlgorithm(dataForm)
			.then(() => {
				ElMessage.success({
					message: '保存成功',
					duration: 500,
					onClose: () => {
						btnLoading.value = false
						visible.value = false
						emit('close')
					}
				})
			})
			.catch(() => {
				btnLoading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
<style lang="less" scoped></style>
